package de.cbf.cam.dsa.facet.talent.edit;

import java.beans.IndexedPropertyChangeEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.swt.widgets.Control;

import de.cbf.cam.dsa.facet.talent.FacetTalent;
import de.cbf.cam.dsa.facet.talent.constants.ConstantsAttributes;
import de.cbf.cam.dsa.facet.talent.constants.ConstantsTalentTypes;
import de.cbf.cam.dsa.facet.talent.helper.BeTypeValue;
import de.cbf.cam.dsa.facet.talent.helper.CompensationTalentReference;
import de.cbf.util.controller.ControllerEvent;
import de.cbf.util.controller.IListenerController;
import de.cbf.util.entity.facet.edit.composites.factorys.AbstractFacetSectionFactory;
import de.cbf.util.entity.facet.edit.controller.AbstractControllerEditFacet;
import de.cbf.util.entity.facet.persistence.resolver.IServiceResolverEntity;
import de.cbf.util.ui.controller.ControllerListEdit;
import de.cbf.util.ui.controller.ControllerTableEdit;
import de.cbf.util.ui.listener.AbstractUIChangeListener;
import de.cbf.util.ui.listener.IUiChangeListener;
import de.cbf.util.ui.listener.UiChangeEvent;

public class ControllerEditorFacetTalent extends AbstractControllerEditFacet<FacetTalent> {
	private static final Log logger = LogFactory.getLog(ControllerEditorFacetTalent.class);
	
	
	public static final String TEXT_STATE_EVENT_TalentType_WARNING		= FacetTalent.TEXT_UNSET_TalentType;	
	public static final String TEXT_STATE_EVENT_AttributeFirst_WARNING	= FacetTalent.TEXT_UNSET_AttributeFirst;	
	public static final String TEXT_STATE_EVENT_AttributeSecond_WARNING	= FacetTalent.TEXT_UNSET_AttributeSecond;	
	public static final String TEXT_STATE_EVENT_AttributeThird_WARNING	= FacetTalent.TEXT_UNSET_AttributeThird;
	public static final String TEXT_STATE_EVENT_RaiseColumn_ERROR		= "RaiseColumn setting not correct";
	public static final String TEXT_STATE_EVENT_RaiseColumn_WARNING		= FacetTalent.TEXT_UNSET_RaiseColumn;
	public static final String TEXT_STATE_EVENT_EBE_ERROR				= "EBE setting not correct";
	public static final String TEXT_STATE_EVENT_EBE_WARNING				= FacetTalent.TEXT_UNSET_EBE;	
	 	
	protected static final StateEvent STATE_EVENT_TalentType_WARNING		= new StateEvent(TEXT_STATE_EVENT_TalentType_WARNING, StateEvent.STATE_WARNING);	 	
	protected static final StateEvent STATE_EVENT_AttributeFirst_WARNING	= new StateEvent(TEXT_STATE_EVENT_AttributeFirst_WARNING, StateEvent.STATE_WARNING);	 	
	protected static final StateEvent STATE_EVENT_AttributeSecond_WARNING	= new StateEvent(TEXT_STATE_EVENT_AttributeSecond_WARNING, StateEvent.STATE_WARNING);	 	
	protected static final StateEvent STATE_EVENT_AttributeThird_WARNING	= new StateEvent(TEXT_STATE_EVENT_AttributeThird_WARNING, StateEvent.STATE_WARNING); 	
	protected static final StateEvent STATE_EVENT_RaiseColumn_ERROR 		= new StateEvent(TEXT_STATE_EVENT_RaiseColumn_ERROR, StateEvent.STATE_ERROR); 	
	protected static final StateEvent STATE_EVENT_RaiseColumn_WARNING		= new StateEvent(TEXT_STATE_EVENT_RaiseColumn_WARNING, StateEvent.STATE_WARNING); 	
	protected static final StateEvent STATE_EVENT_EBE_ERROR 				= new StateEvent(TEXT_STATE_EVENT_EBE_ERROR, StateEvent.STATE_ERROR); 	
	protected static final StateEvent STATE_EVENT_EBE_WARNING				= new StateEvent(TEXT_STATE_EVENT_EBE_WARNING, StateEvent.STATE_WARNING);
		
	
	protected CompositeEditTalent compositeEditTalent;
	
	protected NumberFormat formatInteger;
	
	protected ControllerEditBeSources			controllerEdit_BeSources;
	protected ControllerListEdit				controllerSpecializationsEdit;
	protected ControllerEditCompensationTalents	controllerCompensationTalentsEdit;
	
	@Override
	protected void init() {
		super.init();
		formatInteger = NumberFormat.getIntegerInstance();
		setControllerEdit_BeSources(new ControllerEditBeSources());
		setControllerSpecializationsEdit(new ControllerEditSpecializations());	
		setControllerCompensationTalentsEdit(new ControllerEditCompensationTalents());		
	}
	
	@Override
	protected AbstractFacetSectionFactory createSectionFactory() {		
		return new SectionFactoryTalent();
	}

	@Override
	protected FacetTalent createNewFacet() {
		return new FacetTalent();
	}

	@Override
	public Class<FacetTalent> getFacetClass() {
		return FacetTalent.class;
	}
	
	public void modifyTalentType(String newTalentType) {	
		if(facet == null) return;	
		facet.setTalentType(newTalentType);		
	}
	
	public void modifyAttributeFirst(String newAttributeFirst) {	
		if(facet == null) return;	
		facet.setAttributeFirst(newAttributeFirst);
	} 
	
	public void modifyAttributeSecond(String newAttributeSecond) {	
		if(facet == null) return;	
		facet.setAttributeSecond(newAttributeSecond);
	}
	
	public void modifyAttributeThird(String newAttributeThird) {	
		if(facet == null) return;	
		facet.setAttributeThird(newAttributeThird);
	}

	public void modifyRaiseColumn(String newRaiseColumn) {	
		if(facet == null) return;	
		Number valueNumber;
		try {
			if(compositeEditTalent != null){
				compositeEditTalent.setDecorationRaiseColumnStatus(false);
			}
			removeStateEvent(STATE_EVENT_RaiseColumn_ERROR);
			valueNumber =  formatInteger.parse(newRaiseColumn);		
			if (valueNumber instanceof Long) {
				Long valueLong = (Long) valueNumber;
				facet.setRaiseColumn(valueLong.intValue());
			}			
		} catch (ParseException e) {
			if(compositeEditTalent != null){
				compositeEditTalent.setDecorationRaiseColumnStatus(true);	
			}
			addStateEvent(STATE_EVENT_RaiseColumn_ERROR);			
		}	
	}
		
	public void modifyBeSources(List<BeTypeValue> newBeTypeValues) {
		if(facet == null) return;
		facet.setBeSources(newBeTypeValues);
	}
	
	public void modifyPureATCombatTalent(Boolean newPureATCombatTalent) {
		if(facet == null) return;	
		facet.setPureATCombatTalent(newPureATCombatTalent);
	}
	
	public void modifySpecializations(List<String> newSpecializations) {	
		if(facet == null) return;	
		facet.setSpecializations(newSpecializations);
	}
	
	public void modifyCompensationTalents(List<CompensationTalentReference> newCompensationTalentRefs) {
		if(facet == null) return;
		facet.setCompensationTalents(newCompensationTalentRefs);
	}
	
	public void updateTalentType(String newTalentType) {
		if(compositeEditTalent != null){
			if(newTalentType != null && ! newTalentType.equals(FacetTalent.UNSET_TalentType)){				
				compositeEditTalent.setTalentType(newTalentType);
				removeStateEvent(STATE_EVENT_TalentType_WARNING);
			}else{
				compositeEditTalent.setTalentType(null);
				if(facet != null){
					addStateEvent(STATE_EVENT_TalentType_WARNING);
				}				
			}				
		}			
	}	
	
	public void updateAttributeFirst(String newAttributeFirst) {
		if(compositeEditTalent != null){
			if(newAttributeFirst != null && ! newAttributeFirst.equals(FacetTalent.UNSET_AttributeFirst)){				
				compositeEditTalent.setAttributeFirst(newAttributeFirst);
				removeStateEvent(STATE_EVENT_AttributeFirst_WARNING);
			}else{
				compositeEditTalent.setAttributeFirst(null);
				if(facet != null){
					addStateEvent(STATE_EVENT_AttributeFirst_WARNING);
				}
			}				
		}			
	}
	
	public void updateAttributeSecond(String newAttributeSecond) {
		if(compositeEditTalent != null){
			if(newAttributeSecond != null && ! newAttributeSecond.equals(FacetTalent.UNSET_AttributeSecond)){				
				compositeEditTalent.setAttributeSecond(newAttributeSecond);
				removeStateEvent(STATE_EVENT_AttributeSecond_WARNING);
			}else{
				compositeEditTalent.setAttributeSecond(null);
				if(facet != null){
					addStateEvent(STATE_EVENT_AttributeSecond_WARNING);
				}
			}				
		}			
	}
	
	public void updateAttributeThird(String newAttributeThird) {
		if(compositeEditTalent != null){
			if(newAttributeThird != null && ! newAttributeThird.equals(FacetTalent.UNSET_AttributeThird)){				
				compositeEditTalent.setAttributeThird(newAttributeThird);
				removeStateEvent(STATE_EVENT_AttributeThird_WARNING);
			}else{
				compositeEditTalent.setAttributeThird(null);
				if(facet != null){
					addStateEvent(STATE_EVENT_AttributeThird_WARNING);
				}
			}				
		}			
	}	
	
	public void updateRaiseColumn(Integer newRaiseColumn) {
		if(compositeEditTalent != null){
			if(newRaiseColumn != null && ! newRaiseColumn.equals(FacetTalent.UNSET_RaiseColumn)){				
				compositeEditTalent.setRaiseColumn(formatInteger.format(newRaiseColumn));
				removeStateEvent(STATE_EVENT_RaiseColumn_WARNING);
			}else{
				compositeEditTalent.setRaiseColumn("");
				if(facet != null){
					addStateEvent(STATE_EVENT_RaiseColumn_WARNING);
				}				
			}				
		}			
	}
	
	public void updatePureATCombatTalent(Boolean newPureATCombatTalent) {
		if(compositeEditTalent != null){
			compositeEditTalent.setPureATCombatTalent(newPureATCombatTalent);					
		}			
	}
	
	
	public void updateBeSources(List<BeTypeValue> newBETypeValues) {
		//because sub controller updates alone there should be no work to be done here
//		if(controllerCompensationTalentsEdit != null){
//			controllerCompensationTalentsEdit.setData(newCompensationTalents);
//		}			
	}
	
	public void updateSpecializations(List<String> newSpecializations) {
		//because sub controller updates alone there should be no work to be done here
//		if(controllerSpecializationsEdit != null){
//			controllerSpecializationsEdit.setList(newSpecializations);
//		}				
	}	
	
	public void updateCompensationTalents(List<CompensationTalentReference> newCompensationTalents) {
		//because sub controller updates alone there should be no work to be done here
//		if(controllerCompensationTalentsEdit != null){
//			controllerCompensationTalentsEdit.setData(newCompensationTalents);
//		}				
	}	
	
	@Override
	protected void updateAll(FacetTalent facet) {		
		logger.trace("Updating all data in FacetTalent with: "+facet);
		updateTalentType			(facet.getTalentType());
		updateAttributeFirst		(facet.getAttributeFirst());
		updateAttributeSecond		(facet.getAttributeSecond());
		updateAttributeThird		(facet.getAttributeThird());
		updateRaiseColumn		 	(facet.getRaiseColumn());
		updateBeSources				(facet.getBeSources());
		updatePureATCombatTalent 	(facet.getPureATCombatTalent());
		updateSpecializations	 	(facet.getSpecializations());
		updateCompensationTalents	(facet.getCompensationTalents());		
	}
	
	@Override
	protected void updateClearUI() {		
		updateTalentType			(null);
		updateAttributeFirst		(null);
		updateAttributeSecond		(null);
		updateAttributeThird		(null);
		updateRaiseColumn			(FacetTalent.UNSET_RaiseColumn);		
		updatePureATCombatTalent	(false);
		updateBeSources				(new ArrayList<BeTypeValue>());
		updateSpecializations		(new ArrayList<String>());		
		updateCompensationTalents	(new ArrayList<CompensationTalentReference>());
	}
	
	@Override
	protected void preSetSection() {
		super.preSetSection();
		if(section != null){
			Control client = section.getClient();
			if (client instanceof CompositeEditTalent) {
				compositeEditTalent = (CompositeEditTalent) client;
				deRegisterUIListeners(compositeEditTalent);				
			}else{
				logger.error("wrong client in section: expected "+CompositeEditTalent.class.getSimpleName()+
						" got: "+client.getClass().getSimpleName());
			}
			if(controllerEdit_BeSources != null){
				controllerEdit_BeSources.setComposite(null);
			}	
			if(controllerSpecializationsEdit != null){
				controllerSpecializationsEdit.setComposite(null);
			}	
			if(controllerCompensationTalentsEdit != null){
				controllerCompensationTalentsEdit.setComposite(null);
			}	
		}		
	}

	@Override
	protected void postSetSection() {
		super.postSetSection();
		Control client = section.getClient();
		if (client instanceof CompositeEditTalent) {
			compositeEditTalent = (CompositeEditTalent) client;
			registerUIListeners(compositeEditTalent);
			if(controllerEdit_BeSources != null){
				controllerEdit_BeSources.setComposite(compositeEditTalent.getCompositeBeSources());
			}	
			if(controllerSpecializationsEdit != null){
				controllerSpecializationsEdit.setComposite(compositeEditTalent.getCompositeSpecializations());
			}	
			if(controllerCompensationTalentsEdit != null){
				controllerCompensationTalentsEdit.setComposite(compositeEditTalent.getCompositeCompensationTalents());
			}				
			provideComboInitData(compositeEditTalent);
		}else{
			logger.error("wrong client in section: expected "+CompositeEditTalent.class.getSimpleName()+
					" got: "+client.getClass().getSimpleName());
		}
	}

	protected void registerUIListeners(CompositeEditTalent compositeEditTalent) {
		compositeEditTalent.registerUIListener(getUilFacetTalent());
	}

	protected void deRegisterUIListeners(CompositeEditTalent compositeEditTalent) {
		compositeEditTalent.deRegisterUIListener(getUilFacetTalent());
	}
	
	/**
	 * method to provide data to combo fields in composite<br>
	 * here for talenttype and attributes
	 * @param compositeEditTalent
	 */
	protected void provideComboInitData(CompositeEditTalent compositeEditTalent) {		
		String[] talentTypes = ConstantsTalentTypes.TALENT_TYPES;
		String[] attributes  = ConstantsAttributes.ATTRIBUTES;
		compositeEditTalent.setPossibleTalentTypes(talentTypes);		
		compositeEditTalent.setPossibleAttributesFirst(attributes);
		compositeEditTalent.setPossibleAttributesSecond(attributes);
		compositeEditTalent.setPossibleAttributesThird(attributes);		
	}

	@Override
	protected void preSetNewFacet() {
		super.preSetNewFacet();
		if(facet != null){
			deRegisterModelListeners(facet);			
		}
		if(controllerEdit_BeSources != null){
			controllerEdit_BeSources.setData(null);
		}	
		if(controllerSpecializationsEdit != null){
			controllerSpecializationsEdit.setList(null);
		}		
		if(controllerCompensationTalentsEdit != null){
			controllerCompensationTalentsEdit.setData(null);
		}		
	}

	@Override
	protected void postSetNewFacet() {
		super.postSetNewFacet();
		if(facet != null){
			registerModelListeners(facet);
			if(controllerEdit_BeSources != null){
				controllerEdit_BeSources.setData(facet.getBeSources());
			}	
			if(controllerSpecializationsEdit != null){
				controllerSpecializationsEdit.setList(facet.getSpecializations());
			}	
			if(controllerCompensationTalentsEdit != null){
				controllerCompensationTalentsEdit.setData(facet.getCompensationTalents());
			}	
		}		
	}

	protected void registerModelListeners(FacetTalent facet) {
		facet.getPropertyChangeSupport().addPropertyChangeListener(getPclFacetTalent());
	}

	protected void deRegisterModelListeners(FacetTalent facet) {
		facet.getPropertyChangeSupport().removePropertyChangeListener(getPclFacetTalent());
	}
	
	protected IUiChangeListener getUilFacetTalent() {
		if(uilFacetTalent == null){
			uilFacetTalent = new UILFacetTalent();
		}
		return uilFacetTalent;
	}
	
	/**
	 * @return the controllerEdit_BeSources edit
	 */
	public ControllerTableEdit<BeTypeValue> getControllerEdit_BeSources() {
		return controllerEdit_BeSources;
	}

	/**
	 * @param newControllerEdit_BeSources the controllerSpecializationsEdit to set
	 */
	public void setControllerEdit_BeSources(ControllerEditBeSources newControllerEdit_BeSources) {
		deRegisterSubControllerBeSourcesListeners(controllerEdit_BeSources);
		controllerEdit_BeSources = newControllerEdit_BeSources;
		registerSubControllerBeSourcesListeners(controllerEdit_BeSources);		
	}	
	
	protected void registerSubControllerBeSourcesListeners(
			ControllerTableEdit<BeTypeValue> subController) {
		if(subController != null){
			subController.registerEventListener(getCelBeSources());
		}
	}

	protected void deRegisterSubControllerBeSourcesListeners(
			ControllerTableEdit<BeTypeValue> subController) {
		if(subController != null){
			subController.deRegisterEventListener(getCelBeSources());
		}		
	}

	
	/**
	 * @return the controllerSpecializationsEdit
	 */
	public ControllerListEdit getControllerSpecializationsEdit() {
		return controllerSpecializationsEdit;
	}

	/**
	 * @param newControllerSpecializationsEdit the controllerSpecializationsEdit to set
	 */
	public void setControllerSpecializationsEdit(ControllerListEdit newControllerSpecializationsEdit) {
		deRegisterSubControllerSpecializationsListeners(controllerSpecializationsEdit);
		controllerSpecializationsEdit = newControllerSpecializationsEdit;
		registerSubControllerSpecializationsListeners(controllerSpecializationsEdit);		
	}	
	
	protected void registerSubControllerSpecializationsListeners(ControllerListEdit subController){
		if(subController != null){
			subController.registerEventListener(getCelSpecializations());
		}
	}
	
	protected void deRegisterSubControllerSpecializationsListeners(ControllerListEdit subController){
		if(subController != null){
			subController.deRegisterEventListener(getCelSpecializations());
		}
	}
	/**
	 * @return the controllerSpecializationsEdit
	 */
	public ControllerTableEdit<CompensationTalentReference> getControllerCompensationTalentsEdit() {
		return controllerCompensationTalentsEdit;
	}

	/**
	 * @param newControllerCompensationTalentsEdit the controllerSpecializationsEdit to set
	 */
	public void setControllerCompensationTalentsEdit(ControllerEditCompensationTalents newControllerCompensationTalentsEdit) {
		deRegisterSubControllerCompensationTalentsListeners(controllerCompensationTalentsEdit);
		controllerCompensationTalentsEdit = newControllerCompensationTalentsEdit;
		registerSubControllerCompensationTalentsListeners(controllerCompensationTalentsEdit);		
	}	
	
	protected void registerSubControllerCompensationTalentsListeners(
			ControllerTableEdit<CompensationTalentReference> subController) {
		if(subController != null){
			subController.registerEventListener(getCelCompensationTalents());
		}
	}

	protected void deRegisterSubControllerCompensationTalentsListeners(
			ControllerTableEdit<CompensationTalentReference> subController) {
		if(subController != null){
			subController.deRegisterEventListener(getCelCompensationTalents());
		}		
	}

	public void setTalentResolver(IServiceResolverEntity serviceResolverTalents) {
		if(controllerCompensationTalentsEdit != null){
			controllerCompensationTalentsEdit.setTalentResolver(serviceResolverTalents);
		}		
	}	

	protected UILFacetTalent uilFacetTalent;
	protected class UILFacetTalent extends AbstractUIChangeListener{
		@Override
		public void uiChanged(UiChangeEvent event) {
			int index = event.getIndex();
			switch (index) {
				case CompositeEditTalent.EVT_EDIT_TALENT_TYPE:{
					Object obj = event.getNewValue();
					if (obj instanceof String) {
						String talentType = (String) obj;
						modifyTalentType(talentType);
					}
					break;
				}
				case CompositeEditTalent.EVT_EDIT_ATTRIBUTE_FIRST:{
					Object obj = event.getNewValue();
					if (obj instanceof String) {
						String attributeFirst = (String) obj;
						modifyAttributeFirst(attributeFirst);
					}
					break;
				}
				case CompositeEditTalent.EVT_EDIT_ATTRIBUTE_SECOND:{
					Object obj = event.getNewValue();
					if (obj instanceof String) {
						String attributeSecond = (String) obj;
						modifyAttributeSecond(attributeSecond);
					}
					break;
				}
				case CompositeEditTalent.EVT_EDIT_ATTRIBUTE_THIRD:{
					Object obj = event.getNewValue();
					if (obj instanceof String) {
						String attributeThird = (String) obj;
						modifyAttributeThird(attributeThird);
					}
					break;
				}
				case CompositeEditTalent.EVT_EDIT_RAISE_COLUMN:{
					Object obj = event.getNewValue();
					if (obj instanceof String) {
						String raiseColumn = (String) obj;
						modifyRaiseColumn(raiseColumn);
					}
					break;
				}				
				case CompositeEditTalent.EVT_EDIT_PURE_AT_COMBAT_TALENT:{
					Object obj = event.getNewValue();
					if (obj instanceof Boolean) {
						Boolean pureATCombatTalent = (Boolean) obj;
						modifyPureATCombatTalent(pureATCombatTalent);
					}
					break;
				}
				default:
					logger.warn("Unknown UI-Changed-Event-Index: "+index);
					break;
			}
		}
	}
	
	protected PCLFacetTalent pclFacetTalent;
	protected PropertyChangeListener getPclFacetTalent() {
		if(pclFacetTalent == null){
			pclFacetTalent = new PCLFacetTalent();
		}
		return pclFacetTalent;
	}	
	
	protected class PCLFacetTalent implements PropertyChangeListener{
		@Override
		public void propertyChange(PropertyChangeEvent evt) {
			if (evt instanceof IndexedPropertyChangeEvent) {
				IndexedPropertyChangeEvent idxEvent = (IndexedPropertyChangeEvent) evt;
				int index = idxEvent.getIndex();
				switch (index) {
					//ignore changes of uuidHeader values and name				
					case FacetTalent.PI_UUID:
					case FacetTalent.PI_DATASOURCE:
					case FacetTalent.PI_DELETED:
					case FacetTalent.PI_NAME:{
						break;
					}
					case FacetTalent.PI_TalentType:{
						if (idxEvent.getNewValue() instanceof String) {
							String talentType = (String) idxEvent.getNewValue();
							updateTalentType(talentType);
						}
						break;
					}		
					case FacetTalent.PI_AttributeFirst:{
						if (idxEvent.getNewValue() instanceof String) {
							String value = (String) idxEvent.getNewValue();
							updateAttributeFirst(value);
						}
						break;
					}	
					case FacetTalent.PI_AttributeSecond:{
						if (idxEvent.getNewValue() instanceof String) {
							String value = (String) idxEvent.getNewValue();
							updateAttributeSecond(value);
						}
						break;
					}		
					case FacetTalent.PI_AttributeThird:{
						if (idxEvent.getNewValue() instanceof String) {
							String value = (String) idxEvent.getNewValue();
							updateAttributeThird(value);
						}
						break;
					}		
					case FacetTalent.PI_RaiseColumn:{
						if (idxEvent.getNewValue() instanceof Integer) {
							Integer value = (Integer) idxEvent.getNewValue();
							updateRaiseColumn(value);
						}
						break;
					}	
					case FacetTalent.PI_BeSources:{
						if (idxEvent.getNewValue() instanceof List) {
							List<?> value = (List<?>) idxEvent.getNewValue();
							List<BeTypeValue> valueList = new ArrayList<BeTypeValue>();
							for (Object obj : value) {
								if (obj instanceof BeTypeValue) {
									BeTypeValue beSource = (BeTypeValue) obj;
									valueList.add(beSource);
								}
							}
							updateBeSources(valueList);
						}
						break;
					}		
					case FacetTalent.PI_PureATCombatTalent:{
						if (idxEvent.getNewValue() instanceof Boolean) {
							Boolean value = (Boolean) idxEvent.getNewValue();
							updatePureATCombatTalent(value);
						}
						break;
					}						
					case FacetTalent.PI_Specializations:{
						if (idxEvent.getNewValue() instanceof List) {
							List<?> value = (List<?>) idxEvent.getNewValue();
							List<String> valueList = new ArrayList<String>();
							for (Object obj : value) {
								if (obj instanceof String) {
									String string = (String) obj;
									valueList.add(string);
								}
							}
							updateSpecializations(valueList);
						}
						break;
					}		
					case FacetTalent.PI_CompensationTalents:{
						if (idxEvent.getNewValue() instanceof List) {
							List<?> value = (List<?>) idxEvent.getNewValue();
							List<CompensationTalentReference> valueList = new ArrayList<CompensationTalentReference>();
							for (Object obj : value) {
								if (obj instanceof CompensationTalentReference) {
									CompensationTalentReference string = (CompensationTalentReference) obj;
									valueList.add(string);
								}
							}
							updateCompensationTalents(valueList);
						}
						break;
					}		
					default:
						logger.warn("Unknown PCL-Index: "+index);
						break;
				}
			}
		}
	}

	protected CELSpecializations celSpecializations;
	protected IListenerController getCelSpecializations() {
		if(celSpecializations == null){
			celSpecializations = new CELSpecializations();
		}
		return celSpecializations;
	}	
	protected class CELSpecializations implements IListenerController{
		@Override
		public int handleControllerEvent(ControllerEvent event) {
			if(ControllerListEdit.CTRL_EVT_CODE_LIST_UPDATED.equals(event.getEventCode())){
				List<String> specializations = new ArrayList<String>();
				List<Object> parameters = event.getParameters();
				for (Object object : parameters) {
					if (object instanceof List) {
						List<?> list = (List<?>) object;
						for (Object listObject : list) {
							if (listObject instanceof String) {
								String string = (String) listObject;
								specializations.add(string);								
							}
						}
					}
				}
				modifySpecializations(specializations);
			}
			return 0;
		}		
	}
	

	protected CELCompensationTalents celCompensationTalents;	
	protected IListenerController getCelCompensationTalents() {
		if(celCompensationTalents == null){
			celCompensationTalents = new CELCompensationTalents();
		}
		return celCompensationTalents;
	}
	protected class CELCompensationTalents implements IListenerController{
		@Override
		public int handleControllerEvent(ControllerEvent event) {
			if(ControllerTableEdit.CTRL_EVT_CODE_TABLE_UPDATED.equals(event.getEventCode())){
				List<CompensationTalentReference> compensationTalentRefs = new ArrayList<CompensationTalentReference>();
				List<Object> parameters = event.getParameters();
				for (Object object : parameters) {
					if (object instanceof List) {
						List<?> list = (List<?>) object;
						for (Object listObject : list) {
							if (listObject instanceof CompensationTalentReference) {
								CompensationTalentReference string = (CompensationTalentReference) listObject;
								compensationTalentRefs.add(string);								
							}
						}
					}
				}
				modifyCompensationTalents(compensationTalentRefs);
			}
			return 0;
		}		
	}
	
	protected CEL_BeSources cel_BeSources;	
	protected IListenerController getCelBeSources() {
		if(cel_BeSources == null){
			cel_BeSources = new CEL_BeSources();
		}
		return cel_BeSources;
	}
	protected class CEL_BeSources implements IListenerController{
		@Override
		public int handleControllerEvent(ControllerEvent event) {
			if(ControllerTableEdit.CTRL_EVT_CODE_TABLE_UPDATED.equals(event.getEventCode())){
				List<BeTypeValue> beSources = new ArrayList<BeTypeValue>();
				List<Object> parameters = event.getParameters();
				for (Object object : parameters) {
					if (object instanceof List) {
						List<?> list = (List<?>) object;
						for (Object listObject : list) {
							if (listObject instanceof BeTypeValue) {
								BeTypeValue value = (BeTypeValue) listObject;
								beSources.add(value);								
							}
						}
					}
				}
				modifyBeSources(beSources);
			}
			return 0;
		}		
	}
	
}
